package com.ybwh.springboot2.util;

import com.alibaba.druid.filter.config.ConfigTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class DruidPasswordUtil {
	private static Logger log = LoggerFactory.getLogger(DruidPasswordUtil.class);

	public static final String MAP_PUBLIC_KEY = "publicKey";

	public static final String MAP_PRIVATE_KEY = "privateKey";

	public static final String MAP_PASSWORD = "password";

	/**
	 * 加密
	 *
	 * @param password
	 *            明文密码
	 * @return 加密后的公私钥及密文密码
	 */
	public static Map<String, String> encrypt(String password) {
		Map<String, String> encryptResult = new HashMap<>(3);
		String[] arr;
		try {
			arr = ConfigTools.genKeyPair(512);
			encryptResult.put(MAP_PRIVATE_KEY, arr[0]);
			encryptResult.put(MAP_PUBLIC_KEY, arr[1]);
			encryptResult.put(MAP_PASSWORD, ConfigTools.encrypt(arr[0], password));
			return encryptResult;
		} catch (Exception e) {
			log.error("druid encrypt error !", e);
		}
		return null;
	}

	public static String decrypt(String publicKey, String encryptPassword) {
		try {
			return ConfigTools.decrypt(publicKey, encryptPassword);
		} catch (Exception e) {
			log.error("druid decrypt error !", e);
		}
		return null;
	}

    public static void main(String[] args) {
		Map<String, String> result = DruidPasswordUtil.encrypt("fan1988");
		if (result != null) {
			System.out.println("加密之后的数据库密码password：[" + result.get("password") + "]");
			System.out.println("数据库公钥publicKey：[" + result.get("publicKey") + "]");
			System.out.println(DruidPasswordUtil.decrypt(result.get("publicKey"), result.get("password")));
		}
	}
}
